<%--
  Created by IntelliJ IDEA.
  User: MAX
  Date: 2021-01-06
  Time: 11:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>供应商管理</title>
    <base href="<%=request.getContextPath()%>/"/>
    <link rel="stylesheet" href="webjars/layui/2.5.6/css/layui.css">

</head>
<body>

<%--  查询表单--%>
<form class="layui-form" lay-filter="search" style="width: 1140px; margin: 0 auto">


    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">供应商编号:</label>
        <div class="layui-input-inline">
            <input type="text" name="supplyId" class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">供应商名称:</label>
        <div class="layui-input-inline">
            <input type="text" name="supplyName" class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">供应商分类:</label>
        <div class="layui-input-inline">
            <select name="supclaId" lay-filter="supcla" lay-verify="" id="supcla">
                <option value="0">请选择</option>
<%--                <option value="1">低值</option>--%>
<%--                <option value="2">高值</option>--%>
<%--                <option value="3">无菌</option>--%>
<%--                <option value="4">医药</option>--%>
<%--                <option value="5">被服</option>--%>
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-inline">
        <label class="layui-form-label">审核状态:</label>
        <div class="layui-input-inline">
            <select name="supplyUploadFlag"  lay-filter="supplyUploadFlag" lay-verify="">
                <option value="0">请选择</option>
                <option value="A">未审核</option>
                <option value="B">待审核</option>
                <option value="C">初审通过</option>
                <option value="D">通过</option>
                <option value="E">未审核</option>
            </select>
        </div>
    </div>



    <div class="layui-form-item layui-inline">
        <div class="layui-inline">
            <label class="layui-form-label">资质期限</label>
            <div class="layui-input-inline">
                <input type="text" name="startSupplyDeadline" class="layui-input" id="startDate" placeholder="起始日期">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline">
                <input type="text" name="endSupplyDeadline" class="layui-input" id="endDate" placeholder="截至日期">
            </div>
        </div>
    </div>


    <div class=" layui-form-item layui-inline">
        <button type="button" onclick="search();" class="layui-btn layui-btn-normal"><i class="layui-icon layui-icon-search"></i></button>
    </div>

    <div class=" layui-form-item layui-inline">
        <button type="button" onclick="creat();" class="layui-btn"> <i class="layui-icon">&#xe654;</i></button>
    </div>

</form>

<%-- 数据表格--%>

<div class="layui-container">

    <table id="supplyTb" lay-filter="supplyTb"></table>

    <script type="text/html" id="action">
        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="review">审核</a>
    </script>


</div>




<%-- 添加--%>
<div style="display: none;"  id="create">
    <form class="layui-form" lay-filter="create">
        <input type="reset" style="display: none" id="restAdd" />
        <div class="layui-form-item">
            <label class="layui-form-label">供应商名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="supplyName" class="layui-input"  required  lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属分类:</label>

                <c:forEach items="${supclaList}" var="supcla">
                    <input type="checkbox" name="supclaIds" value="${supcla.supclaId}" title="${supcla.supclaName}">
                </c:forEach>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">输入密码:</label>
            <div class="layui-input-inline">
                <input type="password" name="supplyPwd" class="layui-input" required  lay-verify="required" maxlength="6">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">重复密码:</label>
            <div class="layui-input-inline">
                <input type="password" name="supplyPwdCheck" class="layui-input" required  lay-verify="required" maxlength="6">
            </div>
        </div>

    </form>
</div>










<script src="webjars/layui/2.5.6/layui.js"></script>
<script>

    //加载日期框
    layui.use('laydate', function(){
        let laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#startDate' //指定元素
        });
        laydate.render({
            elem: '#endDate' //指定元素
        });
    });

    layui.use(["table","form","jquery", 'upload', 'layer'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let params = layui.form.val("search");
        table.render({
            id    : "supplyTb",
            elem  : "#supplyTb",
            where : params,
            url   : "supply/searchSupply",
            page: true,
            cols  : [[
                {title: "供应商编号", field: "supplyId"},
                {title: "供应商名称", field: "supplyName"},
                {title: "供应商分类", field: "supclaCode"},
                {title: "资质到期日", field: "supplyDeadline"},
                {title: "审核状态", field: "supplyUploadFlag"},
                {title: "操作", toolbar: "#action"}
            ]],
            done:function (res,curr,count) {
                //审核状态的转换
                $("[data-field='supplyUploadFlag']").children().each(function(){
                    if($(this).text()=='A'){
                        $(this).text("未审核")
                    }else if($(this).text()=='B'){
                        $(this).text("待审核")
                    }else if($(this).text()=='C'){
                        $(this).text("初审通过")
                    }else if($(this).text()=='D'){
                        $(this).text("通过")
                    }else if($(this).text()=='E'){
                        $(this).text("未通过")
                    }
                });
            }
        })

        table.on("tool(supplyTb)",function ({data,event}) {
            switch (event) {
                case "edit" :
                    update(data);
                    break;
                case "review" :
                    review(data);
                    break;
            }
        });


        //下拉框请求数据
        //下拉框可能会用到 New option
        //这个里面的参数说明 第一个是显示的文本，第二个是value值
        //例如：new Option(item.xm, item.id)第一个参数是下拉列表中显示的值  第二个参数是选中传递给后台的值
            $.post({
                url: 'supcla/getSupcla',
                dataType: 'json',
                success: function ({data}) {
                    //使用循环遍历，给下拉列表赋值
                    $.each(data, function (index, value) {
                        // console.log(value.department_id);
                        //console.log(index,value);
                        $('#supcla').append(new Option( value.supclaName,value.supclaId));// 下拉菜单里添加元素
                    });
                    layui.form.render("select");//重新渲染 固定写法
                }
            })

    });


    //条件模糊查询
    function search() {


        layui.use(["table", "form", "jquery"], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.$;
            let params = layui.form.val("search");
            //console.log(params);
            layui.table.reload('supplyTb', {
                where: params
            });

        });
    }

    function creat() {
            layui.use(["layer","jquery","form","table"],function () {
                let layer = layui.layer;
                let $ = layui.$;
                let table = layui.table;
                layer.open({
                    type : 1,
                    area: ['500px', '360px'],
                    skin:'layui-layer-lan',
                    title:'注册信息',
                    content : $("#create"),
                    btn : ["创建","返回"],
                    yes(){

                        let supplyName= $('#create input[name=supplyName]').val();
                        if(supplyName == null || supplyName == ""){
                            layer.msg("请输入供应商名称！");
                            return;
                        }

                        let supclaIds = [];
                        $('#create input[type=checkbox]:checked').each(function() {
                            supclaIds.push($(this).val());
                        });

                        if(supclaIds.length == 0){
                            layer.msg("请选择供应商分类！");
                            return;
                        }

                        let supplyPwd= $('#create input[name=supplyPwd]').val();
                        if(supplyPwd == null || supplyPwd == ""){
                            layer.msg("请输入密码！");
                            return;
                        }

                        if(supplyPwd.length != 6){
                            layer.msg("请输入六位数密码！");
                            return;
                        }

                        let supplyPwdCheck= $('#create input[name=supplyPwdCheck]').val();
                        if(supplyPwdCheck == null || supplyPwdCheck == ""){
                            layer.msg("请输入校验密码！");
                            return;
                        }

                        if(supplyPwdCheck.length != 6){
                            layer.msg("请输入六位数密码！");
                            return;
                        }
                        if(supplyPwdCheck != supplyPwd){
                            layer.msg("密码不一致！");
                            return;
                        }

                        let params = layui.form.val("create");
                        params.supclaIds = supclaIds;
                        console.log(JSON.stringify(params));

                        $.post({
                            url: "supply/createSupply",
                            data: JSON.stringify(params),
                            contentType: 'application/json'
                        })
                            .done(({msg}) => {
                                layui.table.reload("supplyTb");
                                layer.close(layer.index);
                                layer.msg(msg);
                            });
                        $("#restAdd").click();
                    }
                });
            });




        }





</script>
</body>
</html>
